package com.ssy.lingxi.after.sale.handler.notify;

import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.ssy.lingxi.after.sale.api.model.constant.OpenApiMethodTypeEnum;
import com.ssy.lingxi.after.sale.api.model.vo.request.OpenApiCommonVO;
import com.ssy.lingxi.after.sale.api.model.vo.request.ReturnSubmitVerifyVO;
import com.ssy.lingxi.after.sale.api.model.vo.request.api.ApiVerifyVO;
import com.ssy.lingxi.after.sale.service.IReturnGoodsService;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.component.rabbitMQ.constants.MessageConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 *  open-api 退货处理-操作通知处理
 * @author ds
 * @version 2.0.0
 * @date 2021/4/14 18:24
 */
@Component
public class OpenApiMessageHandler {

    @Resource
    private IReturnGoodsService returnService;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @RabbitListener(queues = MessageConstants.OPEN_API_AS_RETURN_QUEUE,ackMode ="MANUAL")
    public void handler(Message message, Channel channel) throws IOException {
        String bodyStr = new String(message.getBody());
        try {
            OpenApiCommonVO request = JSONUtil.toBean(bodyStr, OpenApiCommonVO.class);
            handle(request);
        }catch (Exception e){
            log.error("处理退货处理变更通知异常,body:{},异常消息：",bodyStr,e);
        }

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            log.error("处理退货处理队通知异常,body:{},异常消息：",bodyStr,e);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
    
    private void handle(OpenApiCommonVO req){
        try {
            UserLoginCacheDTO user = new UserLoginCacheDTO();
            user.setMemberId(req.getMemberId());
            user.setMemberRoleId(req.getRoleId());
            if (OpenApiMethodTypeEnum.return_submit_verify.getCode().equals(req.getApiType())) {
                ReturnSubmitVerifyVO returnSubmitVerifyVO = req.getData().toBean(ReturnSubmitVerifyVO.class);
                returnService.submitVerify(user, returnSubmitVerifyVO);
            } else if (OpenApiMethodTypeEnum.return_verify_step_one.getCode().equals(req.getApiType())) {
                ApiVerifyVO apiVerifyVO = req.getData().toBean(ApiVerifyVO.class);
                returnService.verifyStepOne(user,apiVerifyVO);
            } else if (OpenApiMethodTypeEnum.return_verify_step_two.getCode().equals(req.getApiType())) {
                ApiVerifyVO apiVerifyVO = req.getData().toBean(ApiVerifyVO.class);
                returnService.verifyStepTwo(user,apiVerifyVO);
            } else if (OpenApiMethodTypeEnum.return_confirm_verify.getCode().equals(req.getApiType())) {
                ApiVerifyVO apiVerifyVO = req.getData().toBean(ApiVerifyVO.class);
                returnService.confirmVerify(user,apiVerifyVO);
            } else {
                log.warn("无法处理此类请求 apiType:{}",req.getApiType());
            }
        }catch (Exception e){
            log.error("open-api 售后-退货处理异常 req:{}",JSONUtil.toJsonStr(req),e);
        }
    }
}
